import {message} from 'antd';
import {
  queryRule,
  removeRule,
  addRule,
  getPromoCodeList,
  createPromoCode,
  getPromoCodeRewardList,
  getYjxOrgUserInfoList,
  getBackMobileVerCode,
  addYjxCoopUserInfo,
  removeYjxCoopUser,
  getWebRedUserInfo,
  addUserPlat,
  delUserPlat,
  editWebRedUser,
  getPlatInfo,
  editPlat,// 编辑平台资料
} from '../services/api';
import errMassage from '../utils/errMassage';



/**
 * 转发请求 rule
 */
export default {
  namespace: 'rule',

  state: {
    data: {
      list: [],
      pagination: {},
      webReduserInfo :{},
      webRedUserPlat:{},
    },
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryRule, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    * add({ payload, callback }, { call, put }) {
      const response = yield call(addRule, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    * remove({ payload, callback }, { call, put }) {
      const response = yield call(removeRule, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },
    * getPromoCodeList({ payload }, { call, put }) {
      const response = yield call(getPromoCodeList, payload);
      if (response && response.success === 1) {
        yield put({
          type: 'save',
          payload: {
            list: response.data.list,
            pagination: {
              total: response.data.totalPage * response.data.pageSize,
              pageSize: response.data.pageSize,
              current: response.data.currentPage * 1 + 1,
            },
          },
        });
      } else {
        errMassage(response.msg);
      }
    },
    * getPromoCodeRewardList({ payload }, { call, put }) {
      const response = yield call(getPromoCodeRewardList, payload);
      if (response && response.success === 1) {
        yield put({
          type: 'save',
          payload: {
            list: response.data.list,
            pagination: {
              total: response.data.totalPage * response.data.pageSize,
              pageSize: response.data.pageSize,
              current: response.data.currentPage * 1 + 1,
            },
          },
        });
      } else {
        errMassage(response.msg);
      }
    },
    * createPromoCode({ payload }, { call, put }) {
      const response = yield call(createPromoCode, payload);
      if (response && response.success === 1) {
        const response1 = yield call(getPromoCodeList, {
          state: payload.state,
          desc: payload.desc,
          cType: payload.cType,
          pageNum: payload.pageNum,
        });
        if (response1 && response1.success === 1) {
          yield put({
            type: 'save',
            payload: {
              list: response1.data.list,
              pagination: {
                total: response1.data.totalPage * response1.data.pageSize,
                pageSize: response1.data.pageSize,
                current: response1.data.currentPage * 1 + 1,
              },
            },
          });
        } else {
          errMassage(response.msg);
        }
      } else {
        errMassage(response.msg);
      }
    },
    * getYjxOrgUserInfoList({ payload }, { call, put }) {
      const response = yield call(getYjxOrgUserInfoList, payload);
      if (response && response.success === 1) {
        yield put({
          type: 'save',
          payload: {
            list: response.data.list,
            pagination: {
              total: response.data.totalPage * response.data.pageSize,
              pageSize: response.data.pageSize,
              current: response.data.currentPage * 1 + 1,
            },
          },
        });
      } else {
        errMassage(response.msg);
      }
    },
    * GetCode({ payload }, { call }) {
      const response = yield call(getBackMobileVerCode, payload);
      if (response.success !== 1) {
        errMassage(response.msg);
      }
    },
    * addYjxCoopUserInfo({ payload }, { call, put }) {
      const response = yield call(addYjxCoopUserInfo, payload);
      if (response.success === 1) {
        const response1 = yield call(getYjxOrgUserInfoList, payload);
        if (response1.success === 1) {
          yield put({
            type: 'save',
            payload: {
              list: response1.data.list,
              pagination: {
                total: response1.data.totalPage * response1.data.pageSize,
                pageSize: response1.data.pageSize,
                current: response1.data.currentPage * 1 + 1,
              },
            },
          });
          message.success(response1.msg);
        } else {
          errMassage(response1.msg);
        }
      } else {
        errMassage(response.msg);
      }
    },
    *removeYjxCoopUser({payload},{call,put}){
      const response = yield call(removeYjxCoopUser, payload);
      if (response.success === 1) {
        const response1 = yield call(getYjxOrgUserInfoList, {});
        if (response1.success === 1) {
          yield put({
            type: 'save',
            payload: {
              list: response1.data.list,
              pagination: {
                total: response1.data.totalPage * response1.data.pageSize,
                pageSize: response1.data.pageSize,
                current: response1.data.currentPage * 1 + 1,
              },
            },
          });
        } else {
          errMassage(response1.msg);
        }
      } else {
        errMassage(response.msg);
      }
    },
    *getWebRedUserInfo({ payload }, { call, put }) {
      const response = yield call(getWebRedUserInfo, payload);
      if (response.success === 1) {
        yield put({
          type: 'saveWebRedUserInfo',
          payload: response.data.userInfo,
        });
      } else {
        errMassage(response.msg);
      }
    },
    *addUserPlat({ payload }, { call, put }) {// 新增平台资料
      const response = yield call(addUserPlat, payload);
      if (response.success === 1) {
          const response1 = yield call(getWebRedUserInfo, payload);
          if (response1.success === 1) {
            yield put({
              type: 'saveWebRedUserInfo',
              payload: response1.data.userInfo,
            });
            message.success(response.msg)
          } else {
            errMassage(response1.msg);
          }

      } else {
        errMassage(response.msg);
      }
    },
    *editPlat({ payload }, { call, put }) {// 编辑平台资料
      const response = yield call(editPlat, payload);
      if (response.success === 1) {
        const response1 = yield call(getWebRedUserInfo, payload);
        if (response1.success === 1) {
          yield put({
            type: 'saveWebRedUserInfo',
            payload: response1.data.userInfo,
          });
          message.success(response.msg)
        } else {
          errMassage(response1.msg);
        }

      } else {
        errMassage(response.msg);
      }
    },
    *removePlat({ payload }, { call, put }) {// 删除网红用户平台资料
      const response = yield call(delUserPlat, payload);
      if (response.success === 1) {
        const response1 = yield call(getWebRedUserInfo, payload);
        if (response1.success === 1) {
          yield put({
            type: 'saveWebRedUserInfo',
            payload: response1.data.userInfo,
          });
          message.success(response.msg)
        } else {
          errMassage(response1.msg);
        }
      } else {
        errMassage(response.msg);
      }
    },
    *editWebRedUser({ payload }, { call, put }) {// 编辑网红资料
      const response = yield call(editWebRedUser, payload);
      if (response.success === 1) {
        const response1 = yield call(getWebRedUserInfo, payload);
        if (response1.success === 1) {
          yield put({
            type: 'saveWebRedUserInfo',
            payload: response1.data.userInfo,
          });
          message.success(response.msg)
        } else {
          errMassage(response1.msg);
        }
      } else {
        errMassage(response.msg);
      }
    },
    *getPlatInfo({ payload }, { call, put }) {// 获取单个网红平台资料
      const response = yield call(getPlatInfo, payload);
      if (response.success === 1) {
        yield put({
          type: 'saveWebRedUserPlat',
          payload: response.data[0],
        });
      } else {
        errMassage(response.msg);
      }
    },

  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveWebRedUserInfo(state, action) {
      return {
        ...state,
        data: {
          ...state.data,
          webReduserInfo: action.payload,
        },
      };
    },
    saveWebRedUserPlat(state, action) {// 保存平台资料信息
      return {
        ...state,
        data: {
          ...state.data,
          webRedUserPlat: action.payload,
        },
      };
    },

  },
};
